package cn.zust.itcost.service.impl;

import cn.zust.itcost.service.PredictService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.stat.regression.SimpleRegression;

import org.junit.Test;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class PredictServiceImpl implements PredictService {
    @Override
    public double[] arimaForecast(double[] inputData, int numForecasts) {
        // 实现ARIMA时间序列分析并预测后续数值
        // 这里使用ARIMA算法对输入数据进行拟合，并预测后续numForecasts个值
        // 以下是一个简单的ARIMA模型示例，实际使用时需要根据具体情况选择合适的ARIMA库或实现自定义的ARIMA算法

        // 这里假设已经有了输入数据inputData
        // 使用ARIMA算法对inputData进行拟合，并预测numForecasts个值
        double[] forecastValues = new double[numForecasts];
        // 这里只是一个示例，实际情况需要使用合适的ARIMA库来进行计算

        // 假设使用简单线性回归作为示例
        SimpleRegression regression = new SimpleRegression();
        for (int i = 0; i < inputData.length; i++) {
            regression.addData(i + 1, inputData[i]);
        }
        double intercept = regression.getIntercept();
        double slope = regression.getSlope();

        for (int i = 0; i < numForecasts; i++) {
            forecastValues[i] = intercept + slope * (inputData.length + i + 1);
        }

        return forecastValues;
    }

    @Test
    public void testArimaForecast() {
        PredictService predictService = new PredictServiceImpl();

        double[] inputData = {15, 11, 47, 14, 35, 46, 78, 89, 102, 115, 96, 165};
        int numForecasts = 5;

        double[] forecastValues = predictService.arimaForecast(inputData, numForecasts);
        for (int i = 0;i < numForecasts; i++){
            log.info(String.valueOf(forecastValues[i]));
        }

    }
}
